<?php
/**
 * 验证
 * @author wisoch@hotmail.com
 *
 */

class Validate {
	public static function length($string) {
		return mb_strwidth($string, 'UTF-8');
	}
	public function isEmail($str) {
		return preg_match('/^[^\d\-_][\w\-\.]*[^\-_]@[^\-][a-zA-Z\d\-]+[^\-](\.[^\-][a-zA-Z\d\-]*[^\-])*\.[a-zA-Z]{2,3}/', $str);
	}
	public static function isPhone($phone, $type = "cn") {
		return (strlen($phone) == 11 || strlen($phone) == 12) && (preg_match('/^13\d{9}$/', $phone) || preg_match('/^15\d{9}$/', $phone) || preg_match('/^18\d{9}$/', $phone) || preg_match('/^14\d{9}$/', $phone) || preg_match('/^0\d{10}$/', $phone) || preg_match('/^0\d{11}$/', $phone));
	}
	public static function isIp($ip){ 
		return preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" . "(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $ip);
	}
	public static function isDate($var){ 
		return preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/",$var);
	}
	public static function isUserName($var){ 
		return preg_match("/^[a-zA-Z0-9_\.\-]{3,16}$/",$var);
	} 
	public static function isPic($var){ 
		return preg_match("/^[a-zA-Z0-9\-\.]+\.(jpg|jpeg|gif|png)$/",$var);
	}
	public static function isUrl($str){
		return is_string($str)&&preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/",$str);
	}
}
?>
